<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------


/**
 * 呼叫中心控制器
 * @package  htdocs
 * @subpackage  quanliadmin
 * @author   allen.qiang
 */

quanli_require('module/base/common/quanliHttp.class.php');
quanli_require('module/base/customer/quanliCust.class.php');
quanli_require('module/base/customer/custDeliver.class.php');
quanli_require('module/base/customer/custInvoice.class.php');
quanli_require('module/base/promotion/quanliCoupon.class.php');
quanli_require('module/base/system/sysRegion.class.php');
quanli_require('module/func/eai.func.php');

class pa_callcenter {  

    //澳粒订单客服会员名
    private $AL_custID = 'DeOllie';

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1:
                    $this->callcenter();
                    break;

                case 2:
                    $this->customer();
                    break;

                case 3:
                    $this->toolbarIframe();
                    break;

                default:
                    backendError();
                    break;

            }

        } else {

            backendError();

        }

    }

    private function callcenter() {

        global $G_PGLOB;

        if(isset($_REQUEST['billPlatform']) && is_numeric($_REQUEST['billPlatform'])){
            $G_PGLOB->smarty->assign( 'billPlatform' , $_REQUEST['billPlatform'] );
        }

        if(isset($_REQUEST['sign']) && !empty($_REQUEST['sign'])){
            $G_PGLOB->smarty->assign( 'sign' , $_REQUEST['sign'] );
        }
        //大客户订单弹出之前才会弹出地址列表 add by loreta.cheng 2015-06-16 start
        if(isset($_REQUEST['operateType']) && !empty($_REQUEST['operateType'])){
            $G_PGLOB->smarty->assign( 'operateType' , $_REQUEST['operateType'] );
        }
        //大客户订单弹出之前才会弹出地址列表 add by loreta.cheng 2015-06-16 end
        $G_PGLOB->smarty->display('callcenter/callcenter.tpl');

    }

    private function customer() {

        global $G_PGLOB;

        //澳粒订单
        if(isset($_REQUEST['billPlatform']) && $_REQUEST['billPlatform'] == 2){
            $_REQUEST['custID'] = $this->AL_custID;
            $G_PGLOB->smarty->assign('billPlatform', $_REQUEST['billPlatform']);
        }

        //大客户订单弹出之前才会弹出地址列表 add by loreta.cheng 2015-06-16 start
        if(isset($_REQUEST['operateType']) && !empty($_REQUEST['operateType'])){
            $G_PGLOB->smarty->assign('operateType' , $_REQUEST['operateType'] );
        }
        //大客户订单弹出之前才会弹出地址列表 add by loreta.cheng 2015-06-16 end

        if (isset($_REQUEST['pcustID']) || isset($_REQUEST['custID']) || isset($_REQUEST['custname']) || isset($_REQUEST['mobile']) ||
            isset($_REQUEST['phone']) || isset($_REQUEST['email'])) {

            //实例化会员类
            $quanliCust = new quanliCust();

            //实例化收货地址类
            $custDeliver = new custDeliver();

            //实例化发票类
            $custInvoice = new custInvoice();

            //实例化优惠券类
            $quanliCoupon = new quanliCoupon( );

            //实例http类
            $quanliHttp = new quanliHttp();

            //实例地址类
            $sysRegion = new sysRegion( $G_PGLOB->DBCA );

            //查询会员信息
            $customer_array = $quanliCust->getCustomerInfo( $_REQUEST );
            //活动信息

            //mod by 20141030 allen.qiang 解决弹屏无提示的问题---start
            if (isset($_REQUEST['mobile']) && !empty($_REQUEST['mobile'])){
                $activity_info_arr  = $quanliCust->promptInfo($_REQUEST['mobile']);
            }elseif (isset($_REQUEST['custID'])&& !empty($_REQUEST['custID'])){
                $activity_info_arr  = $quanliCust->promptInfo($_REQUEST['custID']);
            }
            //mod by 20141030 allen.qiang 解决弹屏无提示的问题---end

            if($activity_info_arr['code'] == 200){
                $G_PGLOB->smarty->assign('activity_info_arr', $activity_info_arr['data']);
            }

            if ($customer_array) {

                if (isset($customer_array[1])) {

                    $G_PGLOB->smarty->assign_by_ref('customer_list_array', $customer_array);
                    $G_PGLOB->smarty->assign_by_ref('arg_array', $_REQUEST);

                } else {

                    $customer_array = $customer_array[0];

                    //查询参数
                    $arg_array = array(
                        'pcustID' => $customer_array['pcustID']
                    );

                    //查询会员收货地址信息
                    $custDeliver_array = $custDeliver->searchCustDeliver( $arg_array );
                    if($custDeliver_array){
                        $customer_array['custDeliver'] = $custDeliver_array;
                    }

                    //查询会员发票信息
                    $custInvoice_array = $custInvoice->searchCustInvoice( $arg_array );
                    if($custInvoice_array){
                        foreach($custInvoice_array as &$v){
                            if($v['invoice_type'] == 1){
                                $v['invoice_type_name'] = '普通发票';
                            }
                            if($v['invoice_title_type'] == 1){
                                $v['invoice_title_type_name'] = '个人';
                            }elseif($v['invoice_title_type'] == 2){
                                $v['invoice_title_type_name'] = '单位';
                            }
                        }
                        $customer_array['custInvoice'] = $custInvoice_array;
                    }

                    //查询会员优惠券信息
                    $coupon_array = $quanliCoupon->searchCouponCode( $arg_array );
                    $coupon_array_use = array();
                    $coupon_array_no_use = array();
                    $coupon_array_past = array();

                    if($coupon_array){
                        foreach( $coupon_array as &$v ){

                            if( $v['couponType'] == 1 ){
                                $v['couponTypeName'] = '编号礼品卡';
                            }
                            if( $v['couponType'] == 2 ){
                                $v['couponTypeName'] = '账户礼品券';
                            }
                            if( $v['couponType'] == 3 ){
                                $v['couponTypeName'] = '万能码';
                            }

                            $past_sign = ($v['endDate'] < $G_PGLOB->sys_date) ? true : false;

                            if($past_sign){
                                $coupon_array_past[] = $v;
                            }else{
                                if(empty($v['billNo'])){
                                    $coupon_array_no_use[] = $v;
                                }else{
                                    $coupon_array_use[] = $v;
                                }
                            }
                        }
                        $customer_array['ebsigCoupon'] = $coupon_array;
                    }

                    //实例化接口操作类
                    $quanliEai = eai_crm();

                    //会员余额
                    $result_array = $quanliEai->searchCustAccount($customer_array['pcustID'], G_CASH_ID);
                    if($result_array['code'] == 200){
                        $customer_array['cashAmt'] = $result_array['data']['useableAccount'];
                    } else {
                        $customer_array['cashAmt'] = 0;
                    }

                    //会员积分
                    $result_array = $quanliEai->searchCustAccount($customer_array['pcustID'], G_POINTS_ID);
                    if($result_array['code'] == 200){
                        $customer_array['pointsAmt'] = $result_array['data']['useableAccount'];
                    } else {
                        $customer_array['pointsAmt'] = 0;
                    }

                    //查询手机号码归属地
                    $city_id = 110;
                    $city_name = '上海';
                    if ($customer_array['cityid']) {
                        $city_id = $customer_array['cityid'];
                    } else if ($customer_array['mobile']) {

                        //$mobile_array = $quanliHttp->get('http://v.showji.com/locating/showji.com1118.aspx?m=' . $customer_array['mobile'] . '&output=json&timestamp=' . time());

                        $mobile_array = $quanliHttp->get('http://api.k780.com:88/?app=phone.get&phone='.$customer_array['mobile'].'&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json');

                        if ( isset($mobile_array['code']) && $mobile_array['code']==200 && $mobile_array['data']['success']==1 ) {

                            $city_array = explode(',',$mobile_array['data']['result']['att']);
                            if( isset($city_array[2]))
                            {
                                $city_name = $city_array[2];
                            }
                            else
                            {
                                $city_name =  $city_array[1];
                            }

                            $arg_array = array(
                                'name' => $city_name,
                                'region_level' => 2
                            );
                            $region_array = $sysRegion->getAreaManageList( $arg_array );
                            $city_id = $region_array[0]['id'];
                        }
                    }


                    //add by JunHui.Li FUN-BK-0051 20150212 增加会员折扣 start
                    $search_sql = 'SELECT rate FROM cust_rank where rankID= ?';
                    $temp_item_array = &$G_PGLOB->DBCA->stmt_query($search_sql, 's',$customer_array['rankID']);
                    $customer_array['rate']=isset($temp_item_array[0]->rate)?$temp_item_array[0]->rate:'1.00';
                    //add by JunHui.Li FUN-BK-0051 20150212 增加会员折扣 end
                    $G_PGLOB->smarty->assign('city_id', $city_id);
                    $G_PGLOB->smarty->assign_by_ref('city_array', $G_PGLOB->getCache('city'));
                    $G_PGLOB->smarty->assign_by_ref('coupon_array_no_use', $coupon_array_no_use);
                    $G_PGLOB->smarty->assign_by_ref('coupon_array_use', $coupon_array_use);
                    $G_PGLOB->smarty->assign_by_ref('coupon_array_past', $coupon_array_past);
                    $G_PGLOB->smarty->assign_by_ref('customer_array', $customer_array);

                }

            } else {
                $G_PGLOB->smarty->assign_by_ref('arg_array', $_REQUEST);
            }

        }

        $G_PGLOB->smarty->display('callcenter/callcenter_customer.tpl');

    }

    private function toolbarIframe() {

        global $G_PGLOB;

        $G_PGLOB->smarty->display('callcenter/toolbarIframe.tpl');

    }

}